<?php 
  // Include Lib path
  set_include_path('/home/servers/labs.geeek.org/lib/' . PATH_SEPARATOR . get_include_path());

  // this defines some of your basic setup
  include_once 'config.php';
  include_once 'lib.php';

  $user = getUser($_GET['facebook_uid']);
  if (!$user | empty($user->latitude_uid)){
	header('Location:settings.php');
	exit;
  } 

  // Get Mappy Token
  include("gen_token.php"); 
  $token = getToken($mappy_login,$mappy_password);

  // Get Google Latitude Data
  require_once 'Zend/Http/Client.php';
  require_once 'Zend/Json.php';
   
  $client = new Zend_Http_Client();
  $client->setConfig(array('timeout' => 15));
  $client->setUri('http://www.google.com/latitude/apps/badge/api?user='. $user->latitude_uid .'&type=json');
   
  try{ 
     $response =  $client->request('GET');
  } catch(Zend_Http_Client_Adapter_Exception $e){
     die($e->getMessage());
  }

  if ($response->isError()) {
      die($response->getStatus() . ": " . $response->getMessage());
  }
      
  $googleLatitude = Zend_Json::decode($response->getBody());
  $coordinates = $googleLatitude['features'][0]['geometry']['coordinates'];
  $properties =  $googleLatitude['features'][0]['properties'];

  $latitude = $coordinates[0];
  $longitude = $coordinates[1];
  $photoUrl = $properties['photoUrl']; 
  $placardUrl = $properties['placardUrl'];
  $placardWidth = $properties['placardWidth'];
  $placardHeight = $properties['placardHeight'];
  $accuracyInMeters = $properties['accuracyInMeters'];
  $reverseGeocode = $properties['reverseGeocode'];
  $lastUpdateTime = date('D M Y H:i:s',  $properties['timeStamp']);

?>
<html>
<head>
<title>Mappy Latitude Map</title>
<script src="http://axe.mappy.com/1v1/init/get.aspx?auth=<?php echo $token ?>&version=2.02&solution=ajax"></script>
</head>
<body/>
<div id="mymap" style="width:95%;height:60%"></div>

<script>

var latitude = "<?php echo $latitude ?>";
var longitude = "<?php echo $longitude ?>";
var accuracyInMeters = "<?php echo  $accuracyInMeters ?>";
var placardUrl = "<?php echo $placardUrl ?>";
var placardWidth = "<?php echo $placardWidth ?>";
var placardHeight = "<?php echo $placardHeight ?>";
var reverseGeocode = "<?php echo $reverseGeocode ?>";
var lastUpdateTime = "<?php echo $lastUpdateTime ?>";

// Initialize the map
var map = new Mappy.api.map.Map({
   container:'#mymap'
});

// Set the center
map.setCenter(new Mappy.api.geo.Coordinates(latitude,longitude),8);


// Add circle    
var shapeLayer = new Mappy.api.map.layer.ShapeLayer(40);
map.addLayer(shapeLayer);
    
var circle = new Mappy.api.map.shape.Circle(new Mappy.api.geo.Coordinates(latitude,longitude), accuracyInMeters);
shapeLayer.addShape(circle);

var circleStyle = new Mappy.api.map.shape.ShapeStyle({
     lineWidth : 1,
     strokeStyle : "80FF00FF",
     fillStyle : "80000000"
});
    
circle.setStyle(circleStyle);

// Add a marker 
var markerLayer = new Mappy.api.map.layer.MarkerLayer(40);
map.addLayer(markerLayer);

var icon = new Mappy.api.ui.Icon({
        image: placardUrl,
        size: new Mappy.api.types.Size(placardWidth, placardHeight),
        iconAnchor: new Mappy.api.types.Point(placardWidth/2, placardHeight),
        cssClass: "iconClass"
});    

var marker = new Mappy.api.map.Marker(new Mappy.api.geo.Coordinates(latitude,longitude), icon);
marker.addToolTip("Derni&egrave;re mise &agrave; jour : " + lastUpdateTime);
marker.addListener("click", function ()
{
     marker.openPopUp(reverseGeocode);
});
markerLayer.addMarker(marker);


// Toolbar 
var scalePosition = new Mappy.api.map.tools.ToolPosition('lb', new Mappy.api.types.Point(10, 0));
var scale = new Mappy.api.map.tools.Scale(scalePosition);
map.addTool(scale);
 
var minimapPosition = new Mappy.api.map.tools.ToolPosition('lb');
var minimap = new Mappy.api.map.tools.MiniMap(minimapPosition);
map.addTool(minimap);
 
var toolBarPosition = new Mappy.api.map.tools.ToolPosition('rb', new Mappy.api.types.Point(10, 15));
var toolBar = new Mappy.api.map.tools.ToolBar({
    miniMap : { label : "Open miniMap",
                  position : minimapPosition },
    move : { label : "Click to move" },
    zoom : { label : "Zoom in/out" },
    mouseWheelZoom : { label : "Enable/Disable zoom on mousewheel" },
    selection : { label: "Rectangle selection" },
    slider : { label: "Slider" }
}, toolBarPosition);
map.addTool(toolBar);

</script>
</body>
</html>
